package com.zzh.custom.chess;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;

import androidx.annotation.Nullable;

import com.zzh.tools.system.ToolScreen;


public class PiecesPerformView extends View {

    private static final String TAG = PiecesPerformView.class.getSimpleName();

    // 钩子
    public interface OnLoadPointF {
        PointF getPointF(int key);
    }

    private OnLoadPointF mOnLoadPointF;

    public PiecesPerformView(Context context) {
        this(context, null);
    }

    public PiecesPerformView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public PiecesPerformView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    // pieces 点
    private SparseArray<PiecesBean> mPiecesBeans;
    // 画笔
    private Paint mPaint;
    // 选中的key
    private int mSelectKey;
    // 1dp对应的像素值
    private int mDip2px1;

    private void init(Context context, AttributeSet attrs) {
        mDip2px1 = ToolScreen.dip2px(context, 1);
        createPaint();
        resetPieces();
    }

    private void createPaint() {
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(10);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
    }

    private void resetPieces() {
        mPiecesBeans = PiecesFactory.createPiecesS();
    }

    public void setOnLoadPointF(OnLoadPointF onLoadPointF) {
        this.mOnLoadPointF = onLoadPointF;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mOnLoadPointF != null) {
            for (int i = 0; i < mPiecesBeans.size(); i++) {
                PiecesBean piecesBean = mPiecesBeans.valueAt(i);
                PointF pointF = mOnLoadPointF.getPointF(piecesBean.getPointKey());
                if (pointF != null) {
                    Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), piecesBean.getDrawableId());
                    int width = bitmap.getWidth();
                    int height = bitmap.getHeight();
                    canvas.drawBitmap(bitmap, pointF.x - width / 2, pointF.y - height / 2, mPaint);
                    if (mSelectKey == piecesBean.getPointKey()) {
                        canvas.drawCircle(pointF.x, pointF.y, width / 2 + mDip2px1, mPaint);
                    }
                }
            }
        }
    }

    // 选中当前的key
    public void setSelectKey(int selectKey) {
        this.mSelectKey = selectKey;
        invalidate();
    }

    // 去往指定的位置
    public void gotoKey(int key) {
        int selectKey = mSelectKey;
        if (selectKey <= 0) {
            return;
        }
        if (selectKey == key) {
            return;
        }
        PiecesBean piecesBean = mPiecesBeans.get(selectKey);
        if (piecesBean == null) {
            return;
        }
        // 获取属性 判断是不是可以执行
        if (!Pieces.isAllowPerform(selectKey, key, mPiecesBeans)) {
            return;
        }
        // 移除当前棋子
        mPiecesBeans.remove(selectKey);
        // 移除指定被吃棋子
        mPiecesBeans.remove(key);
        // 执行走步
        piecesBean.setPointKey(key);
        mPiecesBeans.put(key, piecesBean);

        mSelectKey = -1;
        invalidate();
        // 判断被将军状况
        int status = Pieces.isTookHold(selectKey, key, mPiecesBeans);
        if (status == 1) {
            // todo 恢复上一步
        } else if (status == 2) {
            // TODO: 2022/7/1 提示将军
        }
        if (Pieces.isKill()) {
            // TODO: 2022/7/1 封死棋盘
        }
    }

    // 获取当前行选中的key
    public int getSelectKey() {
        return mSelectKey;
    }
}
